package com.warm.pump.module.work.controller;

import java.util.HashMap;
import java.util.Map;

import com.warm.pump.module.work.bean.WorkItemDto;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import com.warm.pump.frame.gen.bean.PageBean;
import com.warm.pump.module.work.bean.po.gen.WorkItem;
import com.warm.pump.module.work.service.IWorkItemService;
import com.warm.pump.frame.gen.util.OutJson;
import com.warm.pump.frame.gen.util.MyBeanUtils;
import org.apache.commons.lang3.StringUtils;

/**
 *  work_item
 *
 */
@Controller
@RequestMapping("/workItem")
public class WorkItemController {
	@Autowired
	private IWorkItemService workItemService;

	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("WorkItem:list")
	public OutJson list(Integer page, Integer limit,WorkItem WorkItem,String dtlNameLike){
		Map<String, Object> map = new HashMap<>();
		MyBeanUtils.copyBean2Map(map,WorkItem);
		map.put("orderBy"," exec_num");
		if(StringUtils.isNotEmpty(dtlNameLike)){
			map.put("dtlName_like",dtlNameLike);
		}
		//查询列表数据
		PageBean pageBean = workItemService.getPageBeanByMap(map, page, limit);

		return OutJson.ok().put("page", pageBean);
	}


	/**
	 * 信息
	 */
	@ResponseBody
	@RequestMapping("/info/{id}")
	@RequiresPermissions("WorkItem:info")
	public OutJson info(@PathVariable("id") Long  id){
		WorkItem workItem = workItemService.queryObject(id);

		return OutJson.ok().put("workItem", workItem);
	}


	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	@RequiresPermissions("WorkItem:save")
	public OutJson save(@RequestBody WorkItemDto workItemDto){
		workItemService.save(workItemDto);

		return OutJson.ok();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("WorkItem:update")
	public OutJson update(@RequestBody WorkItemDto workItemDto){
		workItemService.update(workItemDto);

		return OutJson.ok();
	}

	/**
	 * 更新执行顺序
	 */
	@ResponseBody
	@RequestMapping(value="/updateSort", method = RequestMethod.POST)
	public OutJson updateSort(  Long [] ids, Integer[] sorts){
		for(int i = 0;i < ids.length;i++){
			Long id = ids[i];
			Integer execNum = sorts[i];
			WorkItem dtl = new WorkItem();
			dtl.setId(id);
			dtl.setExecNum(execNum);
			workItemService.update(dtl);
		}

		return OutJson.ok();
	}


	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("WorkItem:delete")
	public OutJson delete(@RequestBody Long [] ids){
		workItemService.deleteBatch(ids);

		return OutJson.ok();
	}

	/**
	 * 获取所有bean
	 */
	@ResponseBody
	@RequestMapping("/getProcessServiceBeans")
	public OutJson getProcessServiceBeans(){
		//List<DictItem> list = workItemService.getProcessServiceBeans();
		return OutJson.ok().put("bean",null);
	}

	/**
	 * 通过bean名称获取方法
	 */
	@ResponseBody
	@RequestMapping("/getMethodsByBeanName")
	public OutJson getMethodsByBeanName(String beanName){
		//List<DictItem> list = workItemService.getMethodsByBeanName(beanName);
		return OutJson.ok().put("methods",null);
	}

	@ResponseBody
	@RequestMapping("/defaultValue")
	public OutJson getDefaultValue(@RequestBody WorkItem workItem){
		WorkItem defaultDt = new WorkItem();
		Integer nextOrderNum = workItemService.getNextExecNum(workItem.getWorkId());
		defaultDt.setExecNum(nextOrderNum==null?0:nextOrderNum);
		return OutJson.ok().put("defaultValue",defaultDt);
	}

	/**
	 * 获取所有bean
	 */
	@ResponseBody
	@RequestMapping("/getServiceBeans")
	public OutJson getServiceBeans(){
		//List<DictItem> list = workItemService.getServiceBeans();
		return OutJson.ok().put("bean",null);
	}

	/**
	 * 立即执行任务
	 */
	@ResponseBody
	@RequestMapping("/run")
	public OutJson run(@RequestBody Long[] ids) {
		workItemService.run(ids);
		return OutJson.ok();
	}


}
